using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;

namespace AUSRIS.Reports.Utilities.DataModelReplicator
{
	/// <summary>
	/// The purpose of this application is to copy structure of the "AU" Data Source View and Report Model
	/// to each of the schema-specific .dsv and .smdl files (AU001, AU002, etc.).  This is done by copying
	/// the text of the AU file to each the program specific files, replacing "AU" with the correct schema name.
	/// The same process is repeated for the Report Model files.
	/// </summary>
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				//write header
				Console.ForegroundColor = ConsoleColor.Cyan;
				Console.WriteLine(new string('*', 40));
				Console.WriteLine("AUSRIS Report Data Model Replicator");
				Console.WriteLine("Copyright (C) HQ Air University 2010");
				Console.WriteLine(new string('*', 40));
				Console.ResetColor();
				Console.WriteLine();

				//read in configuration and establish source directory
				WriteStatus("Reading Configuration...");
				string sourcePath = ConfigurationManager.AppSettings["SourcePath"];
				DirectoryInfo sourceDirectory = new DirectoryInfo(Path.GetFullPath(sourcePath));
				if(!sourceDirectory.Exists)
				{
					throw new ApplicationException(string.Format("{0} not found", sourceDirectory));
				}
				WriteSuccess("Using Source Directory: {0}", sourceDirectory.FullName);


				//prompt to continue
				Console.Write("Continue? (Y/N) ");
				ConsoleKeyInfo proceed = Console.ReadKey();
				if ( proceed.Key != ConsoleKey.Y )
				{
					return;
				}

				//process the Data Source Views
				WriteStatus("Processing Data Source Views");
				ProcessFileTypeTemplate("AU", ".dsv", sourceDirectory);
				WriteSuccess("Done with Data Source Views");

				//process the Models
				WriteStatus("Processing Report Models");
				ProcessFileTypeTemplate("AU", ".smdl", sourceDirectory);
				WriteSuccess("Done with Report Models");

				//Done
				Console.WriteLine();
				WriteSuccess("Replication Complete");

			}
			catch ( Exception ex )
			{
				WriteError(ex);
			}
			Console.WriteLine();
			Console.Write("PRESS ENTER TO EXIT");
			Console.ReadLine();
		}

		private static void ProcessFileTypeTemplate(string replacementToken, string extension, DirectoryInfo directory)
		{
            string searchPattern = "*" + extension;
            string templateFile = replacementToken + extension;
			string templateText = null;
			foreach ( FileInfo fileInfo in directory.GetFiles(searchPattern) )
			{
				if ( fileInfo.Name == templateFile )
				{
					WriteStatus("Loading {0}...", templateFile);
					templateText = File.ReadAllText(fileInfo.FullName);
					break;
				}
				throw new ApplicationException(string.Format("{0} not found", templateFile));
			}

			if ( templateText != null )
			{
				foreach ( FileInfo fileInfo in directory.GetFiles(searchPattern) )
				{
					if ( fileInfo.Name != templateFile )
					{
						WriteStatus("Processing {0}...", fileInfo.Name);
						string replacementValue = Path.GetFileNameWithoutExtension(fileInfo.Name);
						string newText = templateText.Replace(replacementToken, replacementValue);
						File.WriteAllText(fileInfo.FullName, newText);
						WriteSuccess("Done");
					}
				}
			}
		}

		private static void WriteError(Exception ex)
		{
			Console.ForegroundColor = ConsoleColor.Red;
			Console.WriteLine(ex.Message);
			Console.WriteLine(ex.ToString());
			Console.ResetColor();
		}

		private static void WriteStatus(string format, params object[] args)
		{
			Console.ForegroundColor = ConsoleColor.White;
			Console.WriteLine();
			Console.WriteLine(format, args);
			Console.ResetColor();
		}

		private static void WriteSuccess(string format, params object[] args)
		{
			Console.ForegroundColor = ConsoleColor.Green;
			Console.WriteLine(format, args);
			Console.ResetColor();
		}
	}
}
